<?php

require_once( '../../libs/smarty.inc' );

require_once( '../../libs/smt_variables.php' );

require_once( '../../libs/Class_DB.php' );

require_once( '../../libs/Class_ERROR.php' );

require_once( '../../libs/login.inc.php' );



define('TEMPORARY_DIR', '../../temporary/');

define('UPLOAD_DIR',	'../files/case/');



if( !in_array( "K", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;



if( $_SERVER['REQUEST_METHOD'] == 'GET' ){

	

	$phase = 'input';

	

	if( isset($_GET['id']) && $_GET['id'] != '' ){

		

		if( is_numeric( $_GET['id'] ) ){

			

			$db = new Class_DB;



			$sql = sprintf("SELECT * FROM D_CASE WHERE id = %s" , $_GET['id'] );

			$res = $db->query( $sql );

			$forms = mysql_fetch_array( $res, MYSQL_ASSOC );

			$forms['case_date'] = str_replace( '-', '/', $forms['case_date']);

			// テンポラリ書出し

			if( $forms['file1'] != '' ){

				$forms['org_name1'] = $forms['file1'];

				$tmp_name = 'file_' . md5( microtime() ) . getext( $forms['file1'] );

				copy( UPLOAD_DIR . $forms['file1'], TEMPORARY_DIR . $tmp_name );

				$forms['temp_name1'] = $tmp_name;

				

			}else{

				$forms['org_name1'] = '';

				$forms['temp_name1'] = '';

			}

			// 公開開始終了日時 分解

			if( strpos( $forms['st_time'], ' ' ) > 0 ){

				$tmp_date = explode( ' ', $forms['st_time'] );

				$forms['st_date'] = $tmp_date[0];

				$tmp_time = explode(':', $tmp_date[1] );

				$forms['st_time'] = $tmp_time[0];

			}

			if( strpos( $forms['ed_time'], ' ' ) > 0 ){

				$tmp_date = explode( ' ', $forms['ed_time'] );

				$forms['ed_date'] = $tmp_date[0];

				$tmp_time = explode(':', $tmp_date[1] );

				$forms['ed_time'] = intval( $tmp_time[0] );

			}



		}

		

	}else{

		$forms['status'] = 1;

		$forms['case_category'] = 1;

		$forms['case_date'] = date("Y/n/d");

	}



}else{

	

	$forms = $_POST;



	//削除

	if( $forms['mode'] == 'delete' ){

		$db = new Class_DB;

		$sql = sprintf( "DELETE FROM D_CASE WHERE id = %u", $forms['id'] );

		$db->query( $sql );

		$phase = 'complete';

		$forms['id'] = '';

		$smarty->assign('message', '削除が完了しました。');

	}



	// コピーして新規登録

	if( $forms['mode'] == 'copy' ){

		$forms['id'] = '';

		$phase = 'input';

	}

	

	// ファイルアップロード

	if( $forms['mode'] == 'upload' ){



		if( is_uploaded_file($_FILES['file1']['tmp_name']) ){

			$tmp_name = 'file_' . md5( microtime() ) . getext( $_FILES['file1']['name']);

			if( move_uploaded_file( $_FILES['file1']['tmp_name'], TEMPORARY_DIR . $tmp_name) ){

				chmod( TEMPORARY_DIR . $tmp_name, 0644);

				resize( TEMPORARY_DIR . $tmp_name, TEMPORARY_DIR . $tmp_name, 109 );

			}

			$forms['temp_name1'] = $tmp_name;

			$forms['org_name1'] = $_FILES['file1']['name'];

		}

		

		$phase = 'input';



	}





	// 入力画面から

	if( $forms['mode'] == 'input' ){

		

		$err = new Class_ERROR;



		$err_mes['case_name']	= $err->check( $forms['case_name'], array("EXIST") );

		$err_mes['case_date']	= $err->check( $forms['case_date'], array("EXIST", "YMD") );

		

		if( $err->clear ){



			$db = new Class_DB;

			

			if( $forms['st_date'] != '' ){

				$st_time = "'" . $forms['st_date'] . ' ' . $forms['st_time'] . ':00:00' . "'";

			}else{

				$st_time = 'NULL';

			}

			if( $forms['ed_date'] != '' ){

				$ed_time = "'" . $forms['ed_date'] . ' ' . $forms['ed_time'] . ':00:00' . "'";

			}else{

				$ed_time = 'NULL';

			}

			

			if( $forms['id'] == '' ){



				$sql = sprintf("INSERT INTO D_CASE( status, case_category, case_name, sub_title, case_date, page_text, st_time, ed_time, r_date, u_date) 

VALUES('%s', %u, '%s', '%s', '%s', '%s', %s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)",

					$forms['status'],

					$forms['case_category'],

					$db->quote( $forms['case_name'] ), 

					$db->quote( $forms['sub_title'] ), 

					$forms['case_date'], 

					$db->quote( $forms['page_text'] ), 

					$st_time, 

					$ed_time

				);

				$db->query( $sql );

				$smarty->assign('message', '登録が完了しました。');

				$id = $db->last_id();



			}else{

				

				$sql = sprintf("UPDATE D_CASE SET status = '%s', case_category = %u, case_name = '%s', sub_title = '%s', case_date = '%s', page_text = '%s', st_time = %s, ed_time = %s, u_date = CURRENT_TIMESTAMP WHERE id = %u",

					$forms['status'],

					$forms['case_category'],

					$db->quote( $forms['case_name'] ), 

					$db->quote( $forms['sub_title'] ), 

					$forms['case_date'], 

					$db->quote( $forms['page_text'] ), 

					$st_time, 

					$ed_time, 

					$forms['id']

				);

				$db->query( $sql );



				$smarty->assign('message', '更新が完了しました。');

				$id = $forms['id'];

			}



			// ファイル処理

			if( isset( $forms['temp_name1'] ) ){

				$fname = $id . getext( $forms['temp_name1'] );

				rename( TEMPORARY_DIR .$forms['temp_name1'], UPLOAD_DIR . $fname );

				$sql = sprintf("UPDATE D_CASE SET file1 = '%s' WHERE id = %u", $fname, $id );

				$db->query( $sql );

			}

			$phase = 'complete';



		}else{

			$phase = 'input';

		}



	}



	

}



$smarty->assign('phase', $phase);

$smarty->assign('case_category', $case_category);

$smarty->assign('status', array(1 => '公開', 0 => '非公開' ));

$smarty->assign('time', array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23) );



if( isset( $forms ) ) $smarty->assign('forms', $forms);

if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);



$smarty->display('web-admin/case_form.html');



// 拡張子取得

function getext( $filename ){

	

	if( strpos( $filename, "." ) ){

		$tmp = explode('.', $filename );

		return '.' . end( $tmp );

	}else{

		return '';

	}

	

}







// 画像リサイズ・保存

function resize( $src, $dest, $width ){

	

	if( !file_exists($src) ) return false;

	

	if( eregi( ".jpg$", $dest ) ){

		$type = "jpeg";

	}elseif( eregi( ".jpeg", $dest ) ){

		$type = "jpeg";

	}elseif( eregi( ".png", $dest ) ){

		$type = "png";

	}else{

		$type = "";

	}



	if( $srcSize = getImageSize( $src ) ){



		$srcWidth  = $srcSize[0];

		$srcHeight = $srcSize[1];



		if( $type == "jpeg" ){

			$srcImage = imageCreateFromJpeg( $src );

		}elseif( $type == "png" ){

			$srcImage = imageCreateFromPng( $src );

		}



	}



	if( $width < $srcWidth ){

		

		$height = $srcHeight * $width / $srcWidth;

		$newImage = imageCreateTrueColor( $width, $height );

		imageCopyResampled( $newImage, $srcImage, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight );

		ImageJPEG($newImage, $dest, 90);

		imageDestroy( $srcImage );

		

	}else{

		// 小さい画像だったらそのままコピー

		copy( $src, $dest );

	}

	

	return;

}



?>



